<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\logCacheRequest;
use \DB;
use Auth;
use Illuminate\Http\Request;
use App\Models\LogCache;

class CacheController extends CommonController
{

    /**
     * 缓存管理列表
     *
     * @return Response
     */
    public function getList()
    {
//        dd(11);
        $lists = LogCache::orderBy('id', 'asc')->paginate(10);

        return view('logcache.list', array('actions' => $this->actions, 'lists' => $lists));
    }



    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = LogCache::orderBy('id', 'asc');

        if (isset($data['name']) && $data['name'] != '') {
            $sql->where('name', 'like', '%' . trim($data['name']) . '%');
        }

        if (isset($data['action']) && $data['action'] != '') {
            $sql->where('action', 'like', '%' . trim($data['action']) . '%');
        }



        $lists = $sql->paginate(10);

        return view('logcache.search')->with("lists", $lists)->with("name", $data['name'])
            ->with("action", $data['action'])->with("actions", $this->actions);

    }

    /**
     *执行删除缓存
     */
    public function postDel(Request $request)
    {
        $data = $request->input();
        $action = $data['action'];
        $url = $this->wjtrUrl . "script/cache/" . $action;
        $result = @curl($url, array());

        if ($result == 1 || $result == '') {
            return AjaxCallbackMessage('缓存删除成功', true);
        } else {
            return AjaxCallbackMessage('缓存清除失败', false);
        }
    }

    /**
     *获取创建一个缓存管理项目视图
     */
    public function getCreate()
    {
        return view('logcache.create');
    }

    /**
     * 创建一个缓存管理项目
     */
    public function postCreate(logCacheRequest $request)
    {
        $data = $request->input();
        unset($data['formType'], $data['_token']);

        $result = logCache::create($data);
        if (is_object($result)) {
            return AjaxCallbackMessage('缓存管理项目创建成功', true, url('cache/list'));
        } else {
            return AjaxCallbackMessage('缓存管理项目创建失败', false);
        }
    }

    /**
     * 获取编辑一个缓存管理项目页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = logCache::where('id', $data['id'])->first();

        return view('logcache.edit')->with('list', $list);
    }

    /**
     * 编辑一个缓存管理项目
     */
    public function postEdit(logCacheRequest $request)
    {
        $data = $request->input();
        unset($data['formType'], $data['_token']);
        $nameExist = LogCache::where('name', $data['name'])->first();

        if (is_object($nameExist) && $nameExist->id != $data['id']) {
            return AjaxCallbackMessage('缓存管理项目编辑失败，该名称已存在', false);
        }

        $actionExist = LogCache::where('action', $data['action'])->first();
        if (is_object($actionExist) && $actionExist->id != $data['id']) {
            return AjaxCallbackMessage('缓存管理项目编辑失败，该action已存在', false);
        }

        LogCache::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('缓存管理项目编辑成功', true, url('cache/list'));
    }
}